﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Achievement.DataModels;
using Ebooom.Leads.Business.AuthorRole;
using Achievement.Common;

namespace Achievement.BLL.Provider
{
    public class AuthorityProvider
    {
        private static readonly AuthorityProvider instance = new AuthorityProvider();
        public static AuthorityProvider Instance
        {
            get { return instance; }
        }
        private AuthorityProvider() { }

        public List<Authority> GetAuthorityList()
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                return CGK.Authority.Where(a=>a.Display).ToList();
            }
        }

        /// <summary>
        /// 权限验证
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="roleCode"></param>
        /// <returns></returns>
        public bool VerifyRole(long uid, long roleCode)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                var user = CGK.Users.Where(u=>u.ID == uid).FirstOrDefault();
                if (user != null && user.Roles != null)
                {
                    long? currentCode = user.Roles;
                    if (currentCode != null)
                    {
                        return AuthorRoleUtils.IsHasRight(roleCode, long.Parse(currentCode.ToString()));
                    }
                }
                return false;
            }
        }

        /// <summary>
        /// 移除权限
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="roleCode"></param>
        /// <returns></returns>
        public int RemoveRole(long uid, long roleCode)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                var user = CGK.Users.Where(u => u.ID == uid).FirstOrDefault();
                if (user != null && user.Roles != null)
                {
                    user.Roles = AuthorRoleUtils.RemoveRoleFromUser(long.Parse(user.Roles.ToString()), roleCode);
                    return CGK.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 设置权限
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="roleCode"></param>
        /// <returns></returns>
        public int SetRole(long uid, long roleCode, string uploadLimit)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                var user = CGK.Users.Where(u => u.ID == uid).FirstOrDefault();
                if (user != null && user.Roles != null)
                {
                    user.Roles = AuthorRoleUtils.SetRoleForUser(long.Parse(user.Roles.ToString()), roleCode);
                    if (!string.IsNullOrEmpty(uploadLimit))
                    {
                        user.UploadSize = uploadLimit.SecureToLong();
                    }
                    return CGK.SaveChanges();
                }
                return 0;
            }
        }

        public int CovertRole(long uid, long roleCode, string uploadLimit)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                var user = CGK.Users.Where(u => u.ID == uid).FirstOrDefault();
                if (user != null && user.Roles != null)
                {
                    user.Roles = roleCode;
                    if (!string.IsNullOrEmpty(uploadLimit))
                    {
                        user.UploadSize = uploadLimit.SecureToLong();
                    }
                    return CGK.SaveChanges();
                }
                return 0;
            }
        }

    }
}
